package ma.platform.server.student.service.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ma.platform.server.core.util.StreamUtils;
import ma.platform.server.student.entity.TbDateCombo;
import ma.platform.server.student.req.QueryDateComboReq;
import ma.platform.server.student.service.TbDateComboService;
import ma.platform.server.student.mapper.TbDateComboMapper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author Administrator
 * @description 针对表【tb_date_combo(每天的套餐信息)】的数据库操作Service实现
 * @createDate 2024-08-27 17:01:05
 */
@Service
public class TbDateComboServiceImpl extends ServiceImpl<TbDateComboMapper, TbDateCombo>
        implements TbDateComboService {


    @Override
    public List<Map<String, TbDateCombo>> listDate(QueryDateComboReq req) {
        List<TbDateCombo> tbDateComboList = list(Wrappers.<TbDateCombo>lambdaQuery()
                .eq(Objects.nonNull(req.getSchoolId()),
                        TbDateCombo::getSchoolId, req.getSchoolId())
                .between(TbDateCombo::getDate, req.getStartTime(), req.getEndTime())
                .eq(TbDateCombo::getComboId, req.getComboId()));
        Map<String, List<TbDateCombo>> groupMap = StreamUtils.getGroupMap(tbDateComboList, item -> DateUtil.format(item.getDate(), "yyyy-MM-dd"));
        List<Map<String, TbDateCombo>> list = Lists.newArrayList();
        //获取groupMap中最长的一个数组
        Optional<List<TbDateCombo>> optional = groupMap.values().stream().max(Comparator.comparingInt(List::size));
        int maxCount = optional.map(List::size).orElse(0);
        List<Map<String, TbDateCombo>> result = new ArrayList<>();
        for (int i = 0; i < maxCount; i++) {
            Map<String, TbDateCombo> map = new HashMap<>();
            for (String date : groupMap.keySet()) {
                List<TbDateCombo> comboList = groupMap.get(date);
                if (i < comboList.size()) {
                    map.put(date, comboList.get(i));
                }
            }
            result.add(map);
        }
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void copyCombo(Long comboId, String currentDate) {
        //获取上周一到上周天
        DateTime lastWeekStart = DateUtil.beginOfWeek(DateUtil.offset(DateUtil.parse(currentDate, "yyyy-MM-dd"), DateField.DAY_OF_WEEK, -7));
        DateTime lastWeekEnd = DateUtil.endOfWeek(DateUtil.offset(DateUtil.parse(currentDate, "yyyy-MM-dd"), DateField.DAY_OF_WEEK, -1));
        List<TbDateCombo> lastWeekList = list(Wrappers.<TbDateCombo>lambdaQuery().between(TbDateCombo::getDate, lastWeekStart, lastWeekEnd)
                .eq(TbDateCombo::getComboId, comboId));
        if (CollectionUtils.isEmpty(lastWeekList)) {
            return;
        }
        lastWeekList.forEach(item -> {
            item.setId(null);
            //获取下周的当天
            item.setDate(DateUtil.offset(item.getDate(), DateField.DAY_OF_WEEK, 14));
        });
        saveBatch(lastWeekList);
    }
}




